package com.hzqy.service.ift;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.hzqy.commons.conf.SystemConfig;
import com.hzqy.commons.utils.ITFUtil;
import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.persistence.dao.itf.ItfDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.service.pms.PmsServiceBean;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.PmsOrgVo;

@Service
public class ItfServiceBean extends GenericServiceBean<CmContentVo, ItfDao, Integer>{

	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private ItfDao itfDao;
	
	@Resource
	private PmsServiceBean pmsServiceBean;
	
	@Override
	public ItfDao getEntityDao() {
		return itfDao;
	}
	
	
	/**
	 * 状态 1：转码成功并上线
	 *     5：等待转码
	 *     6：正在转码
	 *     7：转码失败
	 * 
	 * 如果id不存在直接返回
	 * 如果id存在:
	 *        如果errorcode == 0 成功 注入
	 *        否则失败
	 * 
	 */
	public Map<String, Object> updateMediaCode(String resultJson){
		int flag = 0;
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuffer str = new StringBuffer();
		try {
			JSONObject jsonObj = JSONObject.parseObject(resultJson);
			int f_cc_id = jsonObj.getIntValue("id");
			params.put("f_cc_id", f_cc_id);
			CmContentVo content = itfDao.selectVideoById(f_cc_id);
			if(content == null){//id && type=7 不存在
				str.append("不存在id为 ").append(f_cc_id).append(" 的视频内容!").append(" 接口返回结果=").append(resultJson);
				flag = -1;
			}else{//id && type=7存在
				if(jsonObj.getIntValue("errorcode") == 0){//成功
					str.append(jsonObj.getString("errormsg"));
					
					String mediaCode = jsonObj.getString("mediaCode");
					
					//判断当前状态 非删除状态 可以直接上线
					if(content.getF_cc_status() != 3 && StringUtils.isNotEmpty(mediaCode)){
						params.put("f_cc_status", 1);
					}else{
						params.put("f_cc_status", content.getF_cc_status());
					}
					
					params.put("f_cc_code", jsonObj.getString("mediaCode"));
					params.put("f_cc_obligate1", jsonObj.getString("mediaCode"));
					flag = 1;
				}else{//失败
					str.append("接口返回结果=").append(resultJson);
					params.put("f_cc_status", 7);
					flag = 0;
				}
			}
		} catch (Exception e) {
			//str.append("处理出错! e=").append(e.());
			LOG.error("updateMediaCode", e);
		}
		params.put("f_cc_remark", str.toString());
		itfDao.updateMediaCode(params);
		params.put("flag", flag);
		return params;
	}
	
	/**
	 * http://172.13.0.39:7781/cbweb/interface/getZmjdByMovids.html
	 * http://115.239.134.179:7781/cbweb/interface/getZmjdByMovids.html
	 * http://60.191.70.189:86/cbweb/interface/getZmjdByMovids.html
	 */
	
	public Map<String, Object> getZmjdQueue(int contentid) {
		Map<String, String> result = ITFUtil.post(SystemConfigUtils.getSysConfig("itfService_url1"), "movids=" + contentid);
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String success= result.get("success");
		String msg = result.get("msg");
		if("1".equals(success)){
			//#Umai:PRO/2016120609373670315@STD#-1#-1#-1#-1
			JSONObject json = JSONObject.parseObject(msg);
			if("0".equals(json.get("errorcode"))){
				String queue = json.get("result").toString();
				if(queue.startsWith("#")){
					queue = queue.substring(1);
					String[] queueArr = queue.split("#");
					if(queueArr.length == 5){
						returnMap.put("success", "1");
						returnMap.put("msg", "成功获取转码队列信息");
						int i = 0;
						returnMap.put("playcode", queueArr[i++]);
						returnMap.put("listnumber", queueArr[i++]);
						returnMap.put("listnumbersd", queueArr[i++]);
						returnMap.put("listnumberhd", queueArr[i++]);
						returnMap.put("listnumberfhd", queueArr[i++]);
					}else{
						returnMap.put("success", "0");
						returnMap.put("msg", "result格式有误:" + json.get("result"));
					}
				}else{
					returnMap.put("success", "0");
					returnMap.put("msg", "不存在contentid=" + contentid);
				}
			}else{
				returnMap.put("success", "0");
				returnMap.put("msg", "请求返回失败code:" + json.get("errorcode") + " 错误信息:" + json.get("errormsg"));
			}
		}else{
			returnMap.put("success", "0");
			returnMap.put("msg", "http请求失败:" + msg);
			LOG.error("http请求失败:" + msg);
		}
		return returnMap;
	}


	public Map<String, Object> zbMessageBack(String result) {
		LOG.debug("校园直播审核回调结果: " + result);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", 1);
		
		return resultMap;
	}


	public Map<String, Object> zbDataInsertInterface(String serverid, int orgId) {
		PmsOrgVo orgVo = pmsServiceBean.selectOrg(orgId);
		
		Map<String, String> result = ITFUtil.post(SystemConfigUtils.getSysConfig("zb_interface_insert_url"), String.format("sId=%s&sName=%s&itvId=%s", orgId, orgVo.getF_po_name(), serverid));
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String success= result.get("success");
		String msg = result.get("msg");
		if("1".equals(success)){
			LOG.debug("校园直播开通账号信息新增接口接口返回结果: " + msg);
			JSONObject json = JSONObject.parseObject(msg);
			String _status = json.getString("status");
			//String _msg = json.getString("msg");
			
			returnMap.put("success", ("500".equals(_status) ? "1" : "0"));
		}else{
			returnMap.put("success", "0");
			LOG.error("http请求失败:" + msg);
		}
		return returnMap;
	}


	public Map<String, Object> zbDataUpdatenterface(String serverid, int orgId) {
		PmsOrgVo orgVo = pmsServiceBean.selectOrg(orgId);
		
		Map<String, String> result = ITFUtil.post(SystemConfigUtils.getSysConfig("zb_interface_update_url"), String.format("sId=%s&sName=%s&itvId=%s", orgId, orgVo.getF_po_name(), serverid));
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String success= result.get("success");
		String msg = result.get("msg");
		if("1".equals(success)){
			LOG.debug("校园直播开通账号信息修改接口接口返回结果: " + msg);
			JSONObject json = JSONObject.parseObject(msg);
			String _status = json.getString("status");
			//String _msg = json.getString("msg");
			
			returnMap.put("success", ("500".equals(_status) ? "1" : "0"));
		}else{
			returnMap.put("success", "0");
			LOG.error("http请求失败:" + msg);
		}
		return returnMap;
	}

	
}
